package com.alibaba.citrus.ots.sales.plan.repository;

import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.PageQueryStrategyFactorRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.StrategyFactorRequest;
import com.alibaba.citrus.ots.sales.plan.sdo.StrategyFactorSDO;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.StrategyFactorLoadListRequest;
import com.epoch.app.bcots.sales_plan.strategyfactor.dto.CreateStrategyFactorResponse;
import com.epoch.app.bcots.sales_plan.strategyfactor.dto.LoadStrategyFactorRequest;
import com.epoch.app.bcots.sales_plan.strategyfactor.dto.UpdateStrategyFactorResponse;
import com.epoch.app.bcots.sales_plan.strategyfactor.model.StrategyFactor;
import com.epoch.app.bcots.sales_plan.strategyfactor.service.StrategyFactorService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class StrategyFactorRepositoryImpl implements StrategyFactorRepository {

    @Resource
    private StrategyFactorService strategyFactorService;

    @Override
    @RepositoryInvoker(printLog = true)
    public StrategyFactorSDO queryStrategyFactor(StrategyFactorRequest strategyFactorRequest) {
        LoadStrategyFactorRequest loadStrategyFactorRequest = strategyFactorQueryRequestConverter(strategyFactorRequest);
        StrategyFactor strategyFactor = strategyFactorService.loadStrategyFactor(loadStrategyFactorRequest);
        StrategyFactorSDO strategyFactorSDO = new StrategyFactorSDO();
        BeanUtils.copyProperties(strategyFactor, strategyFactorSDO);
        return strategyFactorSDO;
    }

    //添加策略因子
    @Override
    @RepositoryInvoker(printLog = true)
    public String createStrategyFactor(StrategyFactorSDO strategyFactorSDO) {

        StrategyFactor strategyFactor = new StrategyFactor();
        BeanUtils.copyProperties(strategyFactorSDO, strategyFactor);

        CreateStrategyFactorResponse createStrategyFactorResponse = strategyFactorService.createStrategyFactor(strategyFactor);
        String lastInsertId = createStrategyFactorResponse.getLastInsertId();

        return lastInsertId;
    }

    //查询策略因子
    @Override
    @RepositoryInvoker(printLog = true)
    public PageResult<List<StrategyFactorSDO>> pageQueryStrategyFactorList(PageQueryStrategyFactorRequest pageQueryStrategyFactorRequest) {
        StrategyFactorLoadListRequest listRequest = strategyFactorQueryConverter(pageQueryStrategyFactorRequest);
        Result<List<StrategyFactor>> listResult = strategyFactorService.loadStrategyFactorList(listRequest);
        List<StrategyFactorSDO> resultList = listResult.getResult().stream().map(strategyFactor -> {
            StrategyFactorSDO strategyFactorSDO = new StrategyFactorSDO();
            BeanUtils.copyProperties(strategyFactor, strategyFactorSDO);
            strategyFactorSDO.setLabel(strategyFactor.getFactorName());
            strategyFactorSDO.setValue(strategyFactor.getFactorCode());
            return strategyFactorSDO;
        }).collect(Collectors.toList());
        return PageResult.of(resultList, listResult.getTotal());
    }

    //修改策略因子状态
    @Override
    @RepositoryInvoker(printLog = true)
    public Integer editStrategyFactor(StrategyFactorSDO strategyFactorSDO) {
        StrategyFactor strategyFactor = strategyFactorEditConverter(strategyFactorSDO);
        UpdateStrategyFactorResponse updateStrategyFactorResponse = strategyFactorService.updateStrategyFactor(strategyFactor);
        return updateStrategyFactorResponse.getCount();

    }

    private StrategyFactorLoadListRequest strategyFactorQueryConverter(PageQueryStrategyFactorRequest pageQueryStrategyFactorRequest) {
        StrategyFactorLoadListRequest loadListRequest = new StrategyFactorLoadListRequest();
        BeanUtils.copyProperties(pageQueryStrategyFactorRequest, loadListRequest);
        return loadListRequest;
    }

    private LoadStrategyFactorRequest strategyFactorQueryRequestConverter(StrategyFactorRequest strategyFactorRequest) {
        LoadStrategyFactorRequest loadStrategyFactorRequest = new LoadStrategyFactorRequest();
        loadStrategyFactorRequest.setId(Long.valueOf(strategyFactorRequest.getId()));
        //BeanUtils.copyProperties(strategyFactorRequest, loadStrategyFactorRequest);
        return loadStrategyFactorRequest;
    }

    private StrategyFactor strategyFactorEditConverter(StrategyFactorSDO strategyFactorSDO) {
        StrategyFactor strategyFactor = new StrategyFactor();
        BeanUtils.copyProperties(strategyFactorSDO, strategyFactor);
        return strategyFactor;
    }
}
